package com.wlz.service.impl;

import com.wlz.dao.AccountInfoDao;
import com.wlz.dao.ProductInfoDao;
import com.wlz.service.PayXmlService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author wlz
 * @date 2022-06-01  9:39 下午
 */

@Service
public class PayXmlServiceImpl implements PayXmlService {

	@Autowired
	private AccountInfoDao accountInfoDao;

	@Autowired
	private ProductInfoDao productInfoDao;

	@SuppressWarnings("divzero")
	@Override
	public void pay(String accountId, double money) {

		// 查询 余额
		double blance = accountInfoDao.qryBlanceByUserId(accountId);

		// 余额不足
		if (new BigDecimal(blance).compareTo(new BigDecimal(money)) < 0) {
			throw new RuntimeException("余额不足");
		}

		// 更新余额
		int retVal = accountInfoDao.updateAccountBlance(accountId,money);
		System.out.println( "retVal: " + retVal);

		// 这样调用 是事务 在 内部方法调用 进行传播
		((PayXmlService) AopContext.currentProxy()).updateProductStore(1);

		int i = 1/0;

	}

	@SuppressWarnings("divzero")
	@Override
	public void updateProductStore(Integer productId) {

		try {
			productInfoDao.updateProductInfo(productId);
		}catch (Exception e) {
			throw new RuntimeException("内部异常");
		}

	}

}
